package com.itheima.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.POJO.CheckGroup;
import com.itheima.POJO.Setmeal;
import com.itheima.VO.setmeal_ids;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.setmealMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.service.setmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = setmealService.class)
@Transactional
public class setmealServiceImpl implements setmealService {
    @Autowired
    private setmealMapper setmealMapper;


    public List<CheckGroup> findAllCheckgroup() {
        List<CheckGroup> allCheckgroup = setmealMapper.findAllCheckgroup();
        return allCheckgroup;
    }

    @Override
    public void addSetmeal(setmeal_ids setmeal_ids) {
        try {
            Setmeal setmeal = setmeal_ids.getFormData();
            setmealMapper.addSetmeal(setmeal);
            Integer id = setmeal_ids.getFormData().getId();
            Integer[] checkgroupIds = setmeal_ids.getCheckgroupIds();
            for (Integer checkgroupId : checkgroupIds) {
                HashMap<String, Integer> map = new HashMap<>();
                map.put("setmeal_id",id);
                map.put("checkgroup_id",checkgroupId);
                setmealMapper.addSetmealAndcheckgroup(map);
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw  new RuntimeException(MessageConstant.ADD_SETMEAL_FAIL);
        }

    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> setmealPage= setmealMapper.findPage(queryString);
        return new PageResult(setmealPage.getTotal(),setmealPage.getResult());
    }

    @Override
    public Result delete(Integer setmealId) {
        try {
            setmealMapper.deleteSetmeal_id4setmeal_checkgroup(setmealId);
            setmealMapper.deleteSetmealById(setmealId);
            return  new Result(true,"删除套餐成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,"删除套餐失败!可能出现未知错误,请刷新页面后重试");
    }

    @Override
    public List<Setmeal> findAllSetmeal() {
       List<Setmeal> setmealList= setmealMapper.findAllSetmeal();
        return setmealList;
    }

    @Override
    public Setmeal findSetmealByID(Integer id) {
      Setmeal setmeal=  setmealMapper.findSetmealByID(id);
        return setmeal;
    }

    @Override
    public Map<String, Integer> findAllSetmealCount(List<Setmeal> allSetmeal) {
        // "setmealCount":[{"name":"套餐1","value":10}, {"name":"套餐2","value":30}, {"name":"套餐3","value":25} ]
        HashMap<String, Integer> hashMap = new HashMap<>();
        for (Setmeal setmeal : allSetmeal) {
          Integer count=  setmealMapper.findCountBySetmealID(setmeal.getId());
          hashMap.put(setmeal.getName(),count);
        }
        return hashMap;
    }

}
